package com.springBoot.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

/**
 * @author chenkuan
 * @version v1.0
 * @desc 字符串工具类
 * @date 2019/3/22 022 16:50
 */
public final class StringUtil {

    /**
     * 将null或空字符串转为null,并去掉末尾空格
     *
     * @param str 字符串
     */
    public static String convertToNull(String str) {
        return StringUtils.isEmpty(str) ? null : str.trim();
    }

    /**
     * 字符串左边补上0
     *
     * @param str       字符串
     * @param strLength 字符串总长度
     */
    public static String stringComZeroLeft(String str, int strLength) {
        Objects.requireNonNull(str, "str不能为null");
        if (str.length() >= strLength) {
            return str;
        }
        StringBuilder builder = new StringBuilder(str);
        while (builder.length() < strLength) {
            builder.insert(0, "0");
        }
        return builder.toString();
    }
}
